package com.woniuxy.carlife.carlifecommonsweb.swagger;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @Title:SwaggerConfig
 * @ProjectName:finance
 * @Description swagger的具体配置
 * @author anada
 * @Date 2021/4/4,21:14
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    // http://localhost:8080/swagger-ui.html	原访问路径
    // http://localhost:8080/doc.html   		友好体验版的访问路径

    //swagger2的配置文件，这里可以配置swagger2的一些基本的内容，比如扫描的包等等


    @Autowired
    private SwaggerProperties swaggerProperties;


    @Bean
    public Docket getDocket(){

        //swagger2配置token（将token放在请求头中）
        List<Parameter> ps = Arrays.asList(
                new ParameterBuilder().required(false)
                    .modelRef(new ModelRef("String"))
                    .parameterType("header")
                    .name("token").description("Token参数")
                    .build()
        );



        //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
        ApiInfo apiInfo = new ApiInfoBuilder()
                //创建人
//                .contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getHost(), swaggerProperties.getContact().getEmail()))
                .contact(new Contact(swaggerProperties.getName(), swaggerProperties.getHost(), swaggerProperties.getEmail()))
                // 描述
                .description(swaggerProperties.getDescription())
                //页面标题
                .title(swaggerProperties.getTitle())
                //版本号
                .version(swaggerProperties.getVersion())
                .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2);

        return  docket.apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(ps);
    }

}
